﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using VCC.Lib.Paging;
using System.Linq.Dynamic;

namespace VCC.Model
{
    public class ProjectManager
    {
        /*
        public static IList<Project> GetList(int number, int catid,int hotYN, int pubYN, string order, string orderDir)
        {
            var dataContext = new ChoVietDataContext();
            var l = dataContext.Projects
                .Where(a => catid > 0 ? a.CatID == catid : true)
                .Where(a => hotYN > 0 ? a.HotYN == true : true)
                .Where(a => pubYN > 0 ? a.PubYN == true : true)
                .OrderBy(string.Format("{0} {1}", order, orderDir)).AsQueryable();
            var rt = l.Take(number).ToList();
            return rt;
        }
        public static IPagedList<Project> GetList(int? catid, int? cityid, int? distrid, string keyword, int hotYN, int pubYN, string order, string orderDir, int page, int pageSize)
        {
            var dataContext = new ChoVietDataContext();            
            var l = dataContext.Projects
                .Where(a => catid > 0 ? a.CatID == catid : true)
                .Where(a => cityid > 0 ? a.CityID == cityid : true)
                .Where(a => distrid > 0 ? a.DistrictID == distrid : true)                
                .Where(a => hotYN > 0 ? a.HotYN == (hotYN == 1 ? true : false) : true)
                .Where(a => pubYN > 0 ? a.PubYN == (pubYN == 1 ? true : false) : true)
                .Where(a => keyword == string.Empty ? true : (a.Title.Contains(keyword.ToLower()) || (a.Summary.ToLower().Contains(keyword.ToLower()))))
                .OrderBy(string.Format("{0} {1}", order, orderDir)).AsQueryable();
            var returnVal = l.ToPagedList(page, pageSize);
            return returnVal;
        }

        /// <summary>
        /// Created by Vietlv
        /// Created date: 17/09/2011
        /// Over load methord GetList
        /// Description: Get project by companyID
        /// </summary>
        public static IPagedList<Project> GetListOfCompany(int? catid,int? companyID,  int? cityid, int? distrid, string keyword, int hotYN, int pubYN, string order, string orderDir, int page, int pageSize)
        {
            var dataContext = new ChoVietDataContext();
            var listProjectOfCompany = dataContext.ProjectCompanyMaps
                .Where(e => companyID > 0 ? e.CompanyID == companyID : false).Select(e => e.ProjectID).ToList();
            var l = dataContext.Projects
                .Where(a => catid > 0 ? a.CatID == catid : true)
                .Where(e => listProjectOfCompany.Count != null && listProjectOfCompany.Count > 0 ? listProjectOfCompany.Contains(e.ID) : false ) // Nếu companyID  null thì trả về kết quả null
                .Where(a => cityid > 0 ? a.CityID == cityid : true)
                .Where(a => distrid > 0 ? a.DistrictID == distrid : true)
                .Where(a => hotYN > 0 ? a.HotYN == (hotYN == 1 ? true : false) : true)
                .Where(a => pubYN > 0 ? a.PubYN == (pubYN == 1 ? true : false) : true)
                .Where(a => keyword == string.Empty ? true : (a.Title.Contains(keyword.ToLower()) || (a.Summary.ToLower().Contains(keyword.ToLower()))))
                .OrderBy(string.Format("{0} {1}", order, orderDir)).AsQueryable();
            var returnVal = l.ToPagedList(page, pageSize);
            return returnVal;
        }

        public static IPagedList<Project> GetList(int? catid, int? cityid, int? distrid,int? companyid, string keyword, int hotYN, int pubYN, string order, string orderDir, int page, int pageSize)
        {
            var dataContext = new ChoVietDataContext();
            var l = dataContext.Projects
                .Where(a => catid > 0 ? a.CatID == catid : true)
                .Where(a => cityid > 0 ? a.CityID == cityid : true)
                .Where(a => companyid > 0 ? a.ProjectCompanyMaps.Where(e=>e.CompanyID == companyid).ToList().Count>0 : true)
                .Where(a => distrid > 0 ? a.DistrictID == distrid : true)
                .Where(a => hotYN > 0 ? a.HotYN == (hotYN == 1 ? true : false) : true)
                .Where(a => pubYN > 0 ? a.PubYN == (pubYN == 1 ? true : false) : true)
                .Where(a => keyword == string.Empty ? true : (a.Title.Contains(keyword.ToLower()) || (a.Summary.ToLower().Contains(keyword.ToLower()))))
                .OrderBy(string.Format("{0} {1}", order, orderDir)).AsQueryable();
            var returnVal = l.ToPagedList(page, pageSize);
            return returnVal;
        }
        public static IList<Comment> GetComments(int id, bool? publishYN, int number)
        {
            var dataContext = new ChoVietDataContext();
            var l = (from c in dataContext.Comments
                     from m in dataContext.CommentsInProjects
                     where c.ID == m.CommentID && m.ProjectID == id && (publishYN == null ? true : c.PublishYN == publishYN)
                     orderby c.ID descending
                     select c).Take(number).ToList();
            return l;
        }
        public static Project GetProject(int pId)
        {
            var db = new ChoVietDataContext();
            return db.Projects.SingleOrDefault(a => a.ID == pId);
        }
        */
    }
}
